package controllers;

import java.util.List;

import models.BookType;

import play.mvc.Controller;
import play.mvc.With;
import service.BookTypeService;

@With(Secure.class)
@CRUD.For(BookType.class)
public class BookTypeaction extends CRUD{
	
	@Check("BookManage ") 
	public static void list(){
		render();
	} 
	
	public static void delS(Long[] ids){
		String succ = "删除失败";
		if(ids != null && ids.length > 0){
			for(Long id : ids){
				try{
					BookType t = BookType.findById(id);
					t.delete();
				}catch(Exception e){}
			}
			succ = "删除成功";
		}else{
			succ = "删除项为空";
		}
		render("/502.html",succ);
	}
	
	public static void edit(BookType bot){
		String str = "";
		try{
			bot.save();	
			str = "1";
		}catch(Exception e){
			e.printStackTrace();
			str = "2";
		}
		render("/BookTypeaction/table.json",str);
	}
	
	public static void save(Long fid,String tname,String tcode){
		BookType t = new BookType();
		t.botName = tname;
		t.botCode = tcode;
		t.fid = fid;
		String str = "";
		try{
			t.save();	
			str = "1";
		}catch(Exception e){
			e.printStackTrace();
			str = "2";
		}
		render("/BookTypeaction/table.json",str);
	}
	
	public static void add(){
		List<BookType> list = BookTypeService.getFtall(null);
		render(list);
	}
	
	public static void ajaxBookType(Long botId){
		List<BookType> list = BookTypeService.getFtall(botId);
		StringBuffer strs = new StringBuffer();
		int i = 0;
		strs.append("[");
		for(BookType t : list){
			strs.append("{");
			strs.append("\"botId\":\""+t.id+"\",");
			strs.append("\"botName\":\""+t.botName+"\",");
			strs.append("\"botCode\":\""+t.botCode+"\",");
			strs.append("\"botLev\":\""+t.botLev+"\"");
			i++;
			if(i >= list.size()){
				strs.append("}");
			}else
				strs.append("},");
		}
		strs.append("]");
		String str = strs.toString();
		render("/BookTypeaction/table.json",str);
	}
	
	public static void tree(Long id){
		List<BookType> trees = BookTypeService.getFtall(id);
		StringBuffer treeD = new StringBuffer();
		treeD.append("[");
		int i = 0 ;
		for(BookType t : trees){
			treeD.append("{ id:'"+t.getId()+"',	name:'"+t.botName+"',	isParent:true}");
			i++;
			if(i < trees.size()){
				treeD.append(",");
			}
		}
		treeD.append("]");
		String s = treeD.toString();
		render(s);
	}
	
	public static void table(String page,String rp,String sortname,String sortorder,String query,String qtype){
		StringBuffer str = new StringBuffer();
		long count =  BookType.count();
		str.append("{\"total\":" + count + ",\"page\":" + page + ",\"rows\":");
		str.append("[");
		try{
			List<BookType> list = BookType.findAll();
			int i = 0;
			for(BookType t : list){
				str.append("{\"id\":\"" + i + "\",\"cell\":");
				str.append("[");
				str.append("\""+t.id+"\",");
				str.append("\""+t.fid+"\",");
				str.append("\""+t.botName+"\",");
				str.append("\""+t.botCode+"\"");
				i++;
				if (i == list.size()) {
					str.append("]");
					str.append("}");
				} else {
					str.append("]");
					str.append("},");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		str.append("]}");
		render(str);
	}

}
